package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.server.model.Admin;
import com.ybkj.daijia.server.service.AdminService;
import com.ybkj.daijia.server.service.BossService;
import com.ybkj.daijia.server.service.OrderService;
import java.util.Map;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Shine
 */
@Controller
@RequestMapping(value = "boss")
public class BossController {

    @Autowired
    private BossService bossService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private AdminService adminService;


    @RequestMapping(value = "reporting", method = RequestMethod.GET)
    public ModelAndView reporting() {
        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("bossreporting")) {
            ModelAndView mv = new ModelAndView();

            mv.setViewName("boss/reporting");

            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "getReportingData", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> getReportingData() {
        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        Admin admin = adminService.findOne(principal.getId());

        if (subject.isPermitted("bossreporting")) {

            Map<String, Object> map = bossService.indexData(admin);

            return map;
        } else {
            throw new NoPermissionException();
        }

    }

}

